<?php
    class Chapter_con extends CI_Controller{
        function __construct() {
            parent::__construct();
        }
        function index(){
            
        }
        function disp_form($id,$filter=''){
            $query_silabus=$this->db->get_where('silabus',array('ID_SILABUS'=>$id));
            $silabus=$query_silabus->row();
            $display['NAMA_SILABUS']=$silabus->NAMA_SILABUS;
            $display['ID_SILABUS']=$silabus->ID_SILABUS;
            if($filter=='Semua'){
                $filter='';
            }else{
                $filter = str_replace('%20', ' ', $filter);
            }
            if($filter!=''){
                $query = $this->db->get_where('topik_silabus',array('KLASIFIKASI'=>$filter));
            }else{
                $query=$this->db->get('topik_silabus');
            }
            if($query->num_rows()>0){
                $display['adatopik']=true;
                foreach ($query->result() as $entry) {
                    $display['pilihan_topik'][$entry->ID_TOPIK]=$entry->JUDUL_TOPIK;
                }
                $this->db->select('KLASIFIKASI');
                $this->db->distinct();
                $query=$this->db->get('topik_silabus');
                $display['pilihan']['Semua']='Semua';
                foreach($query->result() as $row){
                    $display['pilihan'][$row->KLASIFIKASI]=$row->KLASIFIKASI;
                }
                $display['selected']=$filter;
                $query_isi = $this->db->query('SELECT * FROM topik_silabus NATURAL JOIN chapter WHERE ID_SILABUS='.$id.' ORDER BY CHAPTER');
                if($query_isi->num_rows()>0){
                    $this->load->library('table');
                    $this->table->set_template(array('table_open' => '<table border="1" cellpadding="4" cellspacing="3">'));
                    $this->table->set_heading('Chapter','Id Topik', 'Judul Topik','Aksi');
                    foreach ($query_isi->result() as $entry) {
                        $this->table->add_row(
                                $entry->CHAPTER,
                                $entry->ID_TOPIK,
                                anchor(base_url().'admin/subtopik_con/isi_topik/'.$entry->ID_TOPIK,$entry->JUDUL_TOPIK),
                                anchor(base_url().'admin/chapter_con/hapus/'.$entry->ID_SILABUS.'/'.$entry->CHAPTER,'Hapus')
                        );
                    }
                    $display['content']=$this->table->generate();
                }else{
                    $display['content']='Belum ada topik untuk silabus ini';
                }
            }else{
                $display['adatopik']=false;
            }
            
            $data['title']="Silabus ".$display['NAMA_SILABUS'];
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/chapter_form_view', $display);
            $this->load->view('footer_view');
        }
        function insert($id){
            $chapter=$this->input->post('CHAPTER');
            if($chapter!=''){
                $this->db->insert('chapter',$_POST);
                redirect(base_url() . 'admin/chapter_con/disp_form/' . $id);
            }else{
                echo 'harap isi no chapter <br/>';
                echo '<a href="javascript:back()">Kembali</a>';
            }
        }
        function filter(){
            $id=$this->input->post('id');
            $filter=$this->input->post('filter');
            redirect(base_url() . 'admin/chapter_con/disp_form/' . $id.'/'.$filter);
        }
        function hapus($id_silabus,$no_chapter){
            $this->db->where('ID_SILABUS',$id_silabus);
            $this->db->where('CHAPTER',$no_chapter);
            $this->db->delete('chapter');
            redirect(base_url() . 'admin/chapter_con/disp_form/' . $id_silabus);
        }
    }
?>
